<template>
  <el-form :model="form" label-position="top">
    <el-form-item label="标题" prop="title">
      <el-input v-model="form.title" maxlength="20" show-word-limit @input="onConfigChange" />
    </el-form-item>
    <el-form-item label="提示文字" prop="tips">
      <el-input
        type="textarea"
        :rows="4"
        v-model="form.tips"
        maxlength="50"
        show-word-limit
        @input="onConfigChange"
      />
    </el-form-item>
    <el-form-item label="参数数据" prop="columns">
      <CountDataSource v-model="form.columns" @change="onConfigChange" />
    </el-form-item>
    <el-form-item label="计算公式" prop="exportFn">
      <el-input v-model="form.exportFn" @input="onConfigChange" />
    </el-form-item>
    <el-form-item label="是否可编辑" prop="isEdit">
      <el-switch v-model="form.isEdit" @change="onConfigChange" />
    </el-form-item>
    <el-form-item label="是否必填" prop="required">
      <el-switch v-model="form.required" @change="onConfigChange" />
    </el-form-item>
    <el-form-item label="表单ID" prop="key">
      <el-input v-model="form.key" @input="onConfigChange" />
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
const { updateFormItem } = inject<{ updateFormItem: (id: string, config: any) => void }>('forms', {
  updateFormItem: () => {}
})
const props = defineProps<{
  id: string
  tdId: string
  formType: string
  config: any
}>()
let form = reactive({
  title: '',
  tips: '', // 提示语
  required: false, // 是否必填
  key: '', // 表单项key值

  columns: [], // 参数数据
  exportFn: '', // 计算公式
  isEdit: false // 是否可编辑
})
onMounted(() => {
  form = Object.assign(form, props.config)
})
watch(props, () => {
  form = Object.assign(form, props.config)
})
const onConfigChange = () => {
  updateFormItem(props.id, form)
}
</script>
